package engine.logic.login
{
	import engine.data.ResPath;
	import engine.logic.ActSee;
	import engine.logic.loading.LoadingLogic;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.ProgressEvent;
	import flash.utils.getTimer;
	
	import newx.StageInfo;
	import newx.com.loader.SwfLoader;

	public class LoginUI implements ILoginUI
	{
		public function LoginUI()
		{
			_loginContainer.visible = false;
			StageInfo.uiSpace.addChild(_loginContainer);
		}
		
		static public var ShowLogin:String="showlogin";
		static public var ShowRoleCreate:String="showrolecreate";
//		static public var ShowAlert:String="showAlert";
		
		static public var FeedBackLoginResReady:String="feedbackresready";
		static public var FeedBackRoleCreateResReady:String="feedbackresready";
		static public var FeedBackLoign:String="feedbacklogin";
		static public var FeedBackRoleCreate:String="feedbackrolecreate";
		
		private var _roleCreateType:int = 0;
		
		private var _completeCallBack:Function;
		
		public static var _loginContainer:MovieClip=new MovieClip();
		/**背景*/
		private var _bg:MovieClip;
		//=============组合================
		
		private var _loginGroup:LoginGroup;
		
		private var _protectionLogin:ProtectionLogin;
		
		private var _roleCreateGroup:ILoginUI;
		
		private var _loginAlert:LoginAlert;
		
		
		private var _showQueue:Array=[];
		
		private var _actTime	: int;
		
		public static var _backMC:MovieClip
		//===========================================================================
		private var _isLogin:Boolean = false;
		public function showLogin():void
		{
			if(	null	==	_loginGroup)
			{
				initLogin();
			}
			else
			{
				_loginGroup.show();
			}
		}
		
		private function initLogin():void
		{
//			LoadingLogic.getInstance().setProcess(0,"加载登录画面");
			
//			var loader:Loader=new Loader();
//			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loginResCompleteHandler);
//			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loginResProgressHandler);
//			loader.load(new URLRequest("res/swf/Login.swf"));
//			SwfLoader.StartLoad(ResPath.SWF +"back.swf",null,backLoginCompleteHandle,[loginResProgressHandler]);
			SwfLoader.StartLoad(ResPath.SWF+ "Login.swf",null,loginResCompleteHandler,[loginResProgressHandler]);
		}
		private function alertResCompleteHandler(val:DisplayObject,pList:Array):void
		{
			LoginAlert.init(MovieClip(val).loginAlert);
			
			var type:String=_showInfo.type;
			var params:*;
			
			switch(type)
			{
				case LoginUI.ShowLogin:
					this.showLogin();
					break;
				case LoginUI.ShowRoleCreate:
					if(!_isLogin)
					{
						this.showRoleCreate();
						_isLogin = true;
					}
					break;
			}	
		}
		private function loginResCompleteHandler(val:DisplayObject,pList:Array):void
		{
			
//			var mc:MovieClip=LoaderInfo(e.target).content as MovieClip;
			_loginGroup	=	new LoginGroup(val as MovieClip,_loginContainer);
//			_protectionLogin	=	new ProtectionLogin(val as MovieClip,_loginContainer);
			_loginGroup.completeCallBack=loginGroupCb;
			
			if( LoginLogic.Unite_Url == "" )
			{
				_loginGroup.show();
			}
//			else
//			{
//				_loginGroup.bg.visible = false;
//			}
				
//			LoadingLogic.getInstance().forceVisible=false;
			
//			LoadingLogic.getInstance().setProcess(100,"加载登录画面");
			var info:Object	=	new Object();
			info.type=LoginUI.FeedBackLoginResReady;
			this.callCompleteCB(info);
			LoginUI._loginContainer.visible = true;
			
			NxLoaderProxy.setVisible(false);
			StageInfo.bgVisible	=	true;
			
			LoginCallLater.setReady(LoginCallLater.ConfigDataReady);
			LoginCallLater.setReady(LoginCallLater.UiResReady);
			
		}
		private function backLoginCompleteHandle(val:DisplayObject,pList:Array):void
		{
			_backMC		=	val as MovieClip;
			_backMC.x	=	-236;
			_backMC.y	=	-165;
//			_backMC.visible = false;
			NxLoaderProxy.setVisible(false);
			StageInfo.bgVisible	=	true;
			_loginContainer.addChildAt(_backMC,0);
			SwfLoader.StartLoad(ResPath.SWF+ "Login.swf",null,loginResCompleteHandler,[loginResProgressHandler]);
		}
		private function loginResProgressHandler(e:*):void
		{
			if(e is ProgressEvent)
			{
//				LoadingLogic.getInstance().setProcess(100*e.bytesLoaded/e.bytesTotal);
				
				NxLoaderProxy.setProgress(NxLoaderProxy.loginUIS+NxLoaderProxy.loginUIW*e.bytesLoaded/e.bytesTotal,"加载登录界面");
//				NxLoaderProxy.setProgress(e.bytesLoaded*100/e.bytesTotal,"加载登录界面")
			}
		}
		private function loginGroupCb(info:Object):void
		{
			info.type=LoginUI.FeedBackLoign;
			
			this.callCompleteCB(info);	
		}
		
		//=========================================================================
		
		private function showRoleCreate():void
		{
			if(null		==	_roleCreateGroup)
			{
				initRoleCreate();
			}
			else
			{
				_roleCreateGroup.show();
			}
		}
		
		private function initRoleCreate():void
		{
//			var loader:Loader=new Loader();
//			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,roleCreateResCompleteHandler);
//			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,roleCreateResProgressHandler);
//			loader.load(new URLRequest("res/swf/RoleCreate.swf"));
			
//			_roleCreateType = int(ModelLocator.getInstance().mainIni.getContent("RoleCreateUI","type") );
			
//			if(_roleCreateType!=0)
//			{
				SwfLoader.StartLoad(ResPath.SWF+"RoleCreate2.swf",null,roleCreateResCompleteHandler,[roleCreateResProgressHandler]);
//			}
				
//			else
//			{
//				SwfLoader.StartLoad("res/swf/RoleCreate.swf",null,roleCreateResCompleteHandler,[roleCreateResProgressHandler]);
//			}
//			LoadingLogic.getInstance().setProcess(0,"");
			
			_actTime	=	getTimer();
		}
		
		private function roleCreateResCompleteHandler(val:DisplayObject,pList:Array):void
		{
			NxLoaderProxy.setVisible(false);
//			LoadingLogic.getInstance().setProcess(100,"");
			WaitingForScene.end();
			

			var mc:MovieClip=val as MovieClip;
			
//			if(_roleCreateType!=0)
//			{
				_roleCreateGroup = new RoleCreateGroup2(mc,_loginContainer);
				LoginUI._loginContainer.visible = true;
//			}
//			else
//			{
//				_roleCreateGroup=new RoleCreateGroup(mc,_loginContainer);
//			}
			
			_roleCreateGroup.show();
			_roleCreateGroup.completeCallBack=roleCreateCb;
			
			var info:Object	=	new Object();
			info.type=LoginUI.FeedBackRoleCreateResReady;
			this.callCompleteCB(info);
			if(_backMC != null)
			{
				if(_backMC.parent == null)
				{
					_backMC.x	=	-236;
					_backMC.y	=	-165;
					_loginContainer.addChildAt(_backMC,0);
				}
			}
			ActSee.PostLoginStatus(8996, 1, getTimer() - _actTime);
		}
		
		private function roleCreateResProgressHandler(e:*):void
		{
			if(e is ProgressEvent)
			{
				//				LoadingLogic.getInstance().setProcess(100*e.bytesLoaded/e.bytesTotal);
				
				NxLoaderProxy.setProgress(NxLoaderProxy.loginUIS+NxLoaderProxy.loginUIW*e.bytesLoaded/e.bytesTotal,"加载创建角色资源");
				//				NxLoaderProxy.setProgress(e.bytesLoaded*100/e.bytesTotal,"加载登录界面")
			}
//			if( e is ProgressEvent)
//				LoadingLogic.getInstance().setProcess( 100*e.bytesLoaded/e.bytesTotal,"");
		}
		
		private function roleCreateCb(info:Object):void
		{
			info.type=LoginUI.FeedBackRoleCreate;
			this.callCompleteCB(info);
		}
		
		private function callCompleteCB(info:Object):void
		{
			if(null		!=	_completeCallBack)
			{
				_completeCallBack(info);
			}
		}
		
		//=======================================================================
		
//		private function showAlert(str:String):void
//		{
//			if(	null	==	_loginAlert)
//			{
//				initLoginAlert();
//			}
//			else
//			{
//				_loginAlert.showAlert(str);
//			}
//		}
		
//		private function initLoginAlert():void
//		{
//			var loader:Loader=new Loader();
//			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,alertResCompleteHandler);
//			loader.load(new URLRequest("res/swf/LoginAlert.swf"));
//		}
//		
//		private function alertResCompleteHandler(e:Event):void
//		{
//			var mc:MovieClip=LoaderInfo(e.target).content as MovieClip;
//			_loginAlert=new LoginAlert2(mc.loginAlert as MovieClip,_loginContainer);
////			this.callShowQueue();
//		}
		
		//if everything is ready,call at once;
		//if something is ready,try to call
//		private function callShowQueue():void
//		{
//		}
		
//		private function 
		
//		private function initilize(mc:MovieClip):void
//		{
//			StageInfo.uiSpace.addChild(_loginContainer);
//			
//			_bg=mc.getChildByName("loginBG") as MovieClip;
//		}
		
//		private function completeHandler(e:Event):void
//		{
//			LoadingLogic.getInstance().setProcess(100,"登录加载");
//			if(_isNormalLogin)
//			{
//				initilize(LoaderInfo(e.target).content as MovieClip);
//				_loginContainer.addChild(_bg);
//				//			this.showServerList();
//				_serverSelectUI.show();
//				
//				if(null		!=	loadCompleteCallBack)
//				{
//					loadCompleteCallBack();
//				}
//			}
//			else
//			{
//				initilize(LoaderInfo(e.target).content as MovieClip);
//				this._castleSelectUI.show();
//			}
//		}
		
		
		private var _showInfo:Object;
		
		private var _isLoginAlertInit:Boolean	=	false;
		
		/**
		 *  
		 * @param info
		 * 
		 */		
		public function show(info:Object=null):void
		{
			hide();
			
			_showInfo	=	info;
			
			if(	false	==	_isLoginAlertInit)
			{
//				LoadingLogic.getInstance().setProcess(0);
				
//				NxLoaderProxy.setProgress(0);
				SwfLoader.StartLoad(ResPath.SWF+"LoginAlert.swf",null,alertResCompleteHandler);
				
				_isLoginAlertInit	=	true;
			}
			else
			{
				var type:String=_showInfo.type;
				var params:*;
				
				switch(type)
				{
					case LoginUI.ShowLogin:
						this.showLogin();
						break;
					case LoginUI.ShowRoleCreate:
						if(!_isLogin)
						{
							this.showRoleCreate();
							_isLogin = true;
						}
						break;
				}	
			
			}
		}
		
		public function showProtectionLogin():void
		{
			_protectionLogin.show();
		}
		
		public function hide():void
		{
//			trace("------------- 界面隐藏");
		
			if(	null	!=	_loginGroup)
			{
				_loginGroup.hide();
			}
			
			if(	null	!=	_roleCreateGroup)
			{
				_roleCreateGroup.hide();
			}
			if(null != _backMC)
			{
				if( null != _backMC.parent)
				{
					_backMC.parent.removeChild(_backMC);
				}
			}
			if(	null	!=	_protectionLogin)
			{
				_protectionLogin.hide();
			}
			LoginAlert.hide();
		}
		
		public function recycle():void
		{
			if(	null	!=	_loginGroup)
						_loginGroup.recycle();
			
			if(	null	!=	_roleCreateGroup)
						_roleCreateGroup.recycle();
			
			_loginGroup	=	null;
			_roleCreateGroup	=	null;
		}
		
		public function set completeCallBack(cb:Function):void
		{
			_completeCallBack=cb;
		}
	}
}